﻿using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.BLL.WorkProcess;
using Ledict.ConditionExtract.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Ledict.ConditionExtract
{
    /// <summary>
    /// 要素分析窗体
    /// </summary>
    public partial class ElementAnalyze : Form
    {
        //静态锁
        private static readonly object Lock = new object();
        public ElementAnalyze()
        {
            InitializeComponent();

            t_Case_HeadWords.TreeView.AfterExpand += new TreeViewEventHandler(t_Case_HeadWords_AfterExpand);

            t_Global_Court.TreeView.AfterExpand += new TreeViewEventHandler(t_Global_Court_AfterExpand);

        }

        static ElementAnalyze _default;
        public static ElementAnalyze Default
        {
            get
            {
                if (_default == null || _default.IsDisposed)
                {
                    lock (Lock)
                    {
                        if (_default == null || _default.IsDisposed)
                        {
                            _default = new ElementAnalyze();
                        }
                    }

                }
                return _default;
            }
        }




        TreeDataBLL trbll = new TreeDataBLL();

        Ledict.ConditionExtract.BLL.Express_WordBLL wordbll = new Express_WordBLL();
        Ledict.ConditionExtract.BLL.Express_AddElement addelementbll = new Express_AddElement();

        ExpressGroupSituationBLL expresitu = new ExpressGroupSituationBLL();
        Express_MatchSituationBLL match = new Express_MatchSituationBLL();

        AboutRegexBLL aboutreg = new AboutRegexBLL();
        RegexParse.RegexParse getregex = new RegexParse.RegexParse();

        ProcessDataSourceBLL datas = new ProcessDataSourceBLL();
        WorkThread_Public work = new WorkThread_Public();

        private void ElementAnalyze_Load(object sender, EventArgs e)
        {


            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            GetData("t_Case_HeadWords");
            GetData("t_Case_JudicialProcedure");
            GetData("t_Case_Property");
            GetData("t_Global_Court");
            GetData("t_Case_ParagraphType");


        }

        public void GetData(string Tname)
        {
            DataTable dt = trbll.GetTreeHeadWords("", Tname);// 第一层
            DataRow[] arrayDR = dt.Select();

            foreach (DataRow CaseTreeNodeRode in arrayDR)
            {
                TreeNode node = new TreeNode();

                node.Text = CaseTreeNodeRode["name"].ToString() + "(" + CaseTreeNodeRode["id"].ToString() + ")";
                node.Tag = CaseTreeNodeRode["id"];

                switch (Tname)
                {
                    case "t_Case_HeadWords":
                        if (trbll.GetTreeHeadWords(node.Tag.ToString(), Tname).Rows.Count > 0)//第二层
                        {
                            node.Nodes.Add(new TreeNode());
                        }
                        t_Case_HeadWords.TreeView.Nodes.Add(node);
                        break;
                    case "t_Case_JudicialProcedure":
                        t_Case_JudicialProcedure.TreeView.Nodes.Add(node);
                        break;
                    case "t_Case_Property":
                        t_Case_Property.TreeView.Nodes.Add(node);
                        break;
                    case "t_Global_Court":
                        if (trbll.GetTreeHeadWords(node.Tag.ToString(), Tname).Rows.Count > 0)//第二层
                        {
                            node.Nodes.Add(new TreeNode());
                        }
                        t_Global_Court.TreeView.Nodes.Add(node);
                        break;
                    default:
                        t_Case_ParagraphType.TreeView.Nodes.Add(node);
                        break;

                }



            }
        }
        public void GetTreeHeadWordsChild(TreeNode parentNode, string Tname)
        {
            DataTable dt = trbll.GetTreeHeadWords(parentNode.Tag.ToString(), Tname);// 第一层
            DataRow[] arrayDR = dt.Select();

            foreach (DataRow CaseTreeNodeRode in arrayDR)
            {
                TreeNode node = new TreeNode();

                node.Text = LdFormat.ObjectToString(CaseTreeNodeRode["name"]) + "(" + CaseTreeNodeRode["id"] + ")";
                node.Tag = CaseTreeNodeRode["id"];
                if (trbll.GetTreeHeadWords(node.Tag.ToString(), Tname).Rows.Count > 0)//第二层
                {
                    node.Nodes.Add(new TreeNode());
                }
                parentNode.Nodes.Add(node);

            }

        }
        public void t_Case_HeadWords_AfterExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.Nodes.Clear();
            GetTreeHeadWordsChild(e.Node, "t_Case_HeadWords");
        }
        public void t_Global_Court_AfterExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.Nodes.Clear();
            GetTreeHeadWordsChild(e.Node, "t_Global_Court");
        }

        private void StartBtn_Click(object sender, EventArgs e)
        {
            if (t_Case_HeadWords.Text.Trim() != "" && t_Case_ParagraphType.Text.Trim() != "")
            {
                dataPagerMain.ChangePageNumber(1);
            }
            else
            {
                MessageBox.Show("案由、案件类别、裁决程序不可为空，请重新填写!");
                return;
            }
        }

        int startShowIndex;
        int endShowIndex;
        int Nowshowindex = 0;
        DataTable Alldt = new DataTable();
        private void dataPagerMain_PageNumberChanging(object sender, Controls.DataPagerPageIndexChangingEventArgs e)
        {
            var startRowIndex = e.StartRowIndex;
            startShowIndex = startRowIndex + 1;
            var maximumRows = e.MaximumRows;
            var endRowIndex = e.StartRowIndex + e.MaximumRows;
            endShowIndex = endRowIndex;
            #region 查询条件

            DataSourceCondition conditon = new DataSourceCondition();

            conditon.HeadwordId = getregex.GetParseValue("CC1509241028190001", t_Case_HeadWords.Text.Trim());
            conditon.JudicialProcedureId = getregex.GetParseValue("CC1509241028190001", t_Case_JudicialProcedure.Text.Trim());
            conditon.PropertyId = getregex.GetParseValue("CC1509241028190001", t_Case_Property.Text.Trim());
            conditon.CaseIdStart = getregex.GetParseValue("CC1509241028190001", CaseIdStart.Text.Trim());
            conditon.CaseIdEnd = getregex.GetParseValue("CC1509241028190001", CaseIdEnd.Text.Trim());
            conditon.Court = getregex.GetParseValue("CC1509241028190001", t_Global_Court.Text.Trim());
            conditon.ParagraphType = getregex.GetParseValue("CC1509241028190001", t_Case_ParagraphType.Text.Trim());
            conditon.ParagraphkeyWord = getregex.GetParseValue("CC1509241028190001", ParagraphkeyWord.Text.Trim());

            Alldt = datas.GetParagraphLogic(conditon);

            #endregion

            if (Alldt.Rows.Count > 0)
            {
                e.TotalCount = Alldt.Rows.Count;

                DataTable newdt = new DataTable();
                newdt = Alldt.Clone();
                DataRow[] dr = Alldt.Select("showindex>" + startRowIndex + " and showindex<=" + endRowIndex);
                for (int i = 0; i < dr.Length; i++)
                {
                    newdt.ImportRow((DataRow)dr[i]);
                }

                dataGridView1.DataSource = newdt;
            }
            else
            {
                dataGridView1.DataSource = new DataTable();
            }


        }

        private void ToPre_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            richTextBox1.Text = "";
            Nowshowindex--;
            NextPro();
            DataRow[] dr = Alldt.Select("showindex=" + Nowshowindex);
            if (dr.Length > 0)
            {
                string para = dr[0]["ParagraphText"].ToString();
                IList<string> paralist = getregex.GetParseList("CC1509101522300001", para);
                if (paralist.Count > 0)
                {
                    foreach (var item in paralist)
                    {
                        if (item != "")
                        {
                            richTextBox1.Text += item + "\r\n";
                        }

                    }
                }
            }
        }

        private void ToNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            richTextBox1.Text = "";
            Nowshowindex++;
            NextPro();
            DataRow[] dr = Alldt.Select("showindex=" + Nowshowindex);
            if (dr.Length > 0)
            {
                string para = dr[0]["ParagraphText"].ToString();
                IList<string> paralist = getregex.GetParseList("CC1509101522300001", para);
                if (paralist.Count > 0)
                {
                    foreach (var item in paralist)
                    {
                        if (item != "")
                        {
                            richTextBox1.Text += item + "\r\n";
                        }

                    }
                }
            }

        }

        /// <summary>
        /// 定位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LocateWord_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            richTextBox1.SelectionStart = 0;
            richTextBox1.SelectionLength = richTextBox1.Text.Length;
            richTextBox1.SelectionBackColor = Color.White;
            System.Text.RegularExpressions.MatchCollection mc;
            var r = new System.Text.RegularExpressions.Regex(SelectWord.Text.Trim()); //定义一个Regex对象实例
            mc = r.Matches(richTextBox1.Text);
            for (int i = 0; i < mc.Count; i++) //在输入字符串中找到所有匹配
            {
                int start = mc[i].Index; //记录匹配字符的位置
                int length = (SelectWord.Text.Trim()).Length;
                richTextBox1.SelectionStart = start;
                richTextBox1.SelectionLength = length;
                richTextBox1.SelectionBackColor = Color.Red;
            }

            //定位到指定满足条件的第一个文字上
            var firstIndex = richTextBox1.Find(SelectWord.Text.Trim());
            richTextBox1.Focus();
            richTextBox1.SelectionStart = firstIndex;
            richTextBox1.ScrollToCaret();

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            richTextBox1.Text = "";
            string para = dataGridView1.Rows[e.RowIndex].Cells["ParagraphText"].Value.ToString();

            Nowshowindex = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["showindex"].Value.ToString());
            NextPro();
            IList<string> paralist = getregex.GetParseList("CC1509101522300001", para);
            if (paralist.Count > 0)
            {
                foreach (var item in paralist)
                {
                    if (item != "")
                    {
                        richTextBox1.Text += item + "\r\n";
                    }

                }
            }


        }

        public void NextPro()
        {

            if (Nowshowindex <= startShowIndex)
            {
                ToPre.Enabled = false;
            }
            else
            {
                ToPre.Enabled = true;
            }
            if (Nowshowindex >= endShowIndex)
            {
                ToNext.Enabled = false;
            }
            else
            {
                ToNext.Enabled = true;
            }
        }


    }
}
